<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Title</title>
  </head>
  <body>
    <div>aaa</div>
    <script>
      function test01() {
        let sleep = (t) => {
          return new Promise((resolve, reject) => {
            setTimeout(resolve, t || 500);
          });
        };
        // let sum = async function(config) {
        let sum = function (config) {
          let defaultConfig = {
            a1: 1,
            an: 10,
            f: function (x) {
              return x + 1;
            },
          };

          defaultConfig = Object.assign(defaultConfig, config);
          let init = [defaultConfig.f(1), defaultConfig.a1];
          console.log(init);
          let result = 0;
          let next = [];
          // let recur = async (prev) => {
          let recur = (prev) => {
            if (prev[0] <= defaultConfig.an) {
              next = [f(prev[0]), prev[0] + prev[1]];
              console.log(next);

              // await sleep(600)
              // await recur(next);
              recur(next);
            } else {
              console.log("============end=============");
              result = next[1];
            }
          };

          // await recur(init);
          recur(init);

          return result;
        };
        let f = (x) => x * 2 + 1;
        // sum({ f, a1: 1, an: 100 }).then(r=>console.log(r))
        let r = sum({ f, a1: 1, an: 100 });
        console.log(r);
      }
      // test01();
      function test02() {
        //  进行
        let target = [
          { id: 1, label: "zhangsan", pid: 0 },
          { id: 2, label: "lisi", pid: 0 },
          { id: 3, label: "zhangqiang", pid: 1 },
          { id: 4, label: "zhangzhong", pid: 1 },
          { id: 5, label: "lihua", pid: 2 },
          { id: 6, label: "liling", pid: 2 },
          { id: 7, label: "lihuason1", pid: 5 },
          { id: 8, label: "lihuason2", pid: 5 },
        ];
        //
        let p = [
          {
            id: 1,
            label: "zhangsan",
            pid: 0,
            children: [
              { id: 3, label: "zhangqiang", pid: 1, children: [] },
              { id: 4, label: "zhangzhong", pid: 1, children: [] },
            ],
          },
          {
            id: 2,
            label: "lisi",
            pid: 0,
            children: [
              {
                id: 5,
                label: "lihua",
                pid: 2,
                children: [
                  { id: 7, label: "lihuason1", pid: 5, children: [] },
                  { id: 8, label: "lihuason2", pid: 5, children: [] },
                ],
              },
              { id: 6, label: "liling", pid: 2, children: [] },
            ],
          },
        ];

        target.forEach((x) => {
          x["children"] = [];
        });
        console.log(target);
        let init = [];

        let recursive = (prev, pid = 0) => {
          let ss = target.filter((x) => x["pid"] === pid);
          if (ss.length > 0) {
            prev.push(...ss);
            console.log("--", prev);
            prev.forEach((x) => {
              recursive(x.children, x["id"]);
            });

            // else{
            // console.log("递归完毕")
            // }

            // recursive(prev);
          }
        };

        recursive(init,0);
        console.log("==============");
      }
      // test02();
      /*
      *
      * let init =[];
        let i =4;
        let recursive = (prev)=>{
          i--; if (i>0) {

            recursive(prev);

          }
        }
        recursive(init);
        console.log("==============")
      *
      * */

      function test03(){
        let flat2Tree = function(config){
          let defaultConfig = {
            flatData:[],
            rootPid:0,
            keyNameChildren:"children",
            keyNameId:"id",
            keyNamePid:"pid"};
          defaultConfig = Object.assign(defaultConfig,config);
          defaultConfig.flatData = JSON.parse(JSON.stringify(defaultConfig.flatData));
          defaultConfig.flatData.forEach((x) => {
            x[defaultConfig.keyNameChildren] = [];
          });
          console.log(defaultConfig.flatData);
          let init = [];

          let recursive = (prev, pid) => {
            let ss = defaultConfig.flatData.filter((x) => x[defaultConfig.keyNamePid] === pid);
            if (ss.length > 0) {
              console.log(prev);
              prev.push(...ss);
              prev.forEach((x) => {
                recursive(x[defaultConfig.keyNameChildren], x[defaultConfig.keyNameId]);
              });

            }
          };

          recursive(init,defaultConfig.rootPid);
          // console.log("---------",init)
          return init;
        }
        let target = [
          { personId: 1, label: "zhangsan", parentId: -1 },
          { personId: 2, label: "lisi", parentId: -1 },
          { personId: 3, label: "zhangqiang", parentId: 1 },
          { personId: 4, label: "lizhong", parentId: 2 },
          { personId: 5, label: "lihua", parentId: 2 },
          { personId: 6, label: "liling", parentId: 2 },
          { personId: 7, label: "lihuason1", parentId: 5 },
          { personId: 8, label: "lihuason2", parentId: 5 },
        ];
        let rs = flat2Tree({
          flatData:target,
          rootPid:-1,
          keyNameChildren:"childs",
          keyNameId:"personId",
          keyNamePid:"parentId"});
        console.log(rs);
      }
      test03()
    </script>
  </body>
</html>
